//
//  ShowPlayView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/8.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class ShowPlayView: UIView {
    
    public var playInfos:[PlayInfo]? {
        didSet {
            setNeedsLayout()
        }
    }
    
    private var scrollView = UIScrollView()
    // 没有更多数据
    private let nomoredata = NoMoreDataView()
    
    override func layoutSubviews() {
        guard let plays = playInfos else {
            return
        }
        if plays.count == 0 {
            return
        }
        // 记录高度
        var tempHeight:CGFloat = 0
        for i in 0..<plays.count {
            let subview = SubShowPlayView()
            subview.info = plays[i]
            subview.layer.cornerRadius = 10
            subview.backgroundColor = UIColor.white
            subview.frame = CGRect(x: MARGIN, y: MARGIN10 + tempHeight, width: frame.width - MARGIN * 2, height: MARGIN150)
            scrollView.addSubview(subview)
            // 最后一位高度
            tempHeight = subview.frame.maxY + MARGIN5
        }
        // 没有更多数据
        nomoredata.frame = CGRect(x: 0, y: tempHeight, width: frame.width, height: MARGIN55)
        scrollView.addSubview(nomoredata)
        // 设置scrollView属性
        scrollView.showsVerticalScrollIndicator = false
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollView.contentSize = CGSize(width: SCREEN_WIDTH, height: nomoredata.frame.maxY)
        addSubview(scrollView)
    }
    
    
    class SubShowPlayView: UIView {
        
        private let redColor = Palette.colorFromRGB(251, green: 42, blue: 68)
        private let blueColor = Palette.colorFromRGB(61, green: 100, blue: 254)
        
        public var info:PlayInfo?
        
        override func layoutSubviews() {
            // 状态
            let statusView = UILabel(title: "测试", color: UIColor.white, size: 12, align: .center)
            switch info?.status {
            case "未起飞":
                statusView.backgroundColor = blueColor
            case "已延迟":
                statusView.backgroundColor = redColor
            case "已取消":
                statusView.backgroundColor = UIColor.gray
            default:
                break
            }
            statusView.text = info?.status
            statusView.layer.mask = getShape()
            statusView.frame = CGRect(x: 0, y: 0, width: MARGIN50, height: MARGIN20)
            addSubview(statusView)
            // 出发时间
            let cityTime = UILabel(title: "", color: UIColor.black)
            cityTime.text = info?.departtime
            cityTime.font = UIFont.boldSystemFont(ofSize: 29)
            let cityTimeY = statusView.frame.maxY + MARGIN
            cityTime.frame = CGRect(x: MARGIN20, y: cityTimeY, width: MARGIN100, height: MARGIN30)
            addSubview(cityTime)
            // 到达时间
            let endcityTime = UILabel(title: "", color: UIColor.black,align: .right)
            endcityTime.text = info?.arrivaltime
            endcityTime.font = UIFont.boldSystemFont(ofSize: 29)
            let endcityTimeX = frame.width - MARGIN120
            endcityTime.frame = CGRect(x: endcityTimeX, y: cityTimeY, width: MARGIN100, height: MARGIN30)
            addSubview(endcityTime)
            // 出发机场
            let cityTerminalTime = UILabel(title: "", color: UIColor.gray,size: 12)
            cityTerminalTime.text = "\(info!.city!)机场\(info!.departterminal!)"
            let cityTerminalTimeY = cityTime.frame.maxY + MARGIN5
            cityTerminalTime.frame = CGRect(x: MARGIN20, y: cityTerminalTimeY, width: MARGIN100, height: MARGIN)
            addSubview(cityTerminalTime)
            // 到达机场
            let endcityTerminalTime = UILabel(title: "", color: UIColor.gray,size: 12,align: .right)
            endcityTerminalTime.text = "\(info!.endcity!)机场\(info!.departterminal!)"
            endcityTerminalTime.frame = CGRect(x: endcityTimeX, y: cityTerminalTimeY, width: MARGIN100, height: MARGIN)
            addSubview(endcityTerminalTime)
            // 飞机图片
            let playPic = UIImageView(image: UIImage(named: "IMG_GrayAirplane_18x15"))
            playPic.frame.origin = CGPoint(x: cityTime.frame.maxX + MARGIN5, y: cityTime.frame.maxY - MARGIN10)
            addSubview(playPic)
            // 横线
            let lineView = UIView()
            lineView.backgroundColor = COLOR_LINE
            let lineY = playPic.frame.maxY - playPic.bounds.height * 0.5
            let lineW = frame.width - MARGIN140 * 2 - MARGIN10
            lineView.frame = CGRect(x: playPic.frame.maxX + MARGIN5, y: lineY, width: lineW, height: 1)
            addSubview(lineView)
            // 圆点图片
            let cirlePic = UIImageView(image: UIImage(named: "IMG_BookingTripCircle_7x7"))
            cirlePic.frame.origin = CGPoint(x: lineView.frame.maxX + MARGIN5, y: lineY - cirlePic.bounds.height * 0.5)
            addSubview(cirlePic)
            // 下划线
            let line2View = UIView()
            let line2W = frame.width - MARGIN40
            line2View.backgroundColor = COLOR_LINE
            line2View.frame = CGRect(x: MARGIN20, y: cityTerminalTime.frame.maxY + MARGIN, width: line2W, height: 1)
            addSubview(line2View)
            // 航班号、机型
            let flightInfo = UILabel(title: "", color: UIColor.gray,size: 12)
            flightInfo.text = "\(info!.flightnumberid!) | \(info!.cabinname!) | \(info!.craft!) | \(info!.food!) | 行李:\(info!.luggage!)KG"
            let flightInfoH = frame.height - line2View.frame.maxY
            flightInfo.frame = CGRect(x: MARGIN20, y: line2View.frame.maxY, width: line2W, height: flightInfoH)
            addSubview(flightInfo)
        }
        
        //创建图层
        private func getShape() -> CAShapeLayer {
            let shape = CAShapeLayer()
            let round = CGRect(x: 0, y: 0, width: MARGIN50, height: MARGIN20)
            let roundSize = CGSize(width: 10, height: 10)
            let byRound = UIRectCorner(rawValue: UIRectCorner.topLeft.rawValue | UIRectCorner.bottomRight.rawValue)
            shape.path = UIBezierPath.init(roundedRect: round, byRoundingCorners: byRound, cornerRadii: roundSize).cgPath
            return shape
        }
    }
}
